package com.orangelife.service.propFeePollfeeImportLog.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeePollfeeImportLog.IPropFeePollfeeImportLogService;
import com.orangelife.service.propFeePollfeeImportLog.model.PropFeePollfeeImportLogDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeePollfeeImportLog类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-08-31 10:04:09
 * ==============================================================
 */
@Service("propFeePollfeeImportLogService")
public class PropFeePollfeeImportLogServiceImpl extends
		MybaitsGenericDaoSubLogic implements IPropFeePollfeeImportLogService {
	private static Logger logger = Logger
			.getLogger(PropFeePollfeeImportLogServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeePollfeeImportLogServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeePollfeeImportLog(
			PropFeePollfeeImportLogDto propFeePollfeeImportLog)
			throws AppBizException {

		Assert.notNull(propFeePollfeeImportLog);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeePollfeeImportLog.getFileName())) {
			if (propFeePollfeeImportLog.getFileName().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "文件名称长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeePollfeeImportLog.getStatus())) {
			if (propFeePollfeeImportLog.getStatus().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"1导入中,2上传成功，3上传失败长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeePollfeeImportLog.getImportInfo())) {
			if (propFeePollfeeImportLog.getImportInfo().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "上传信息长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeePollfeeImportLog.getLink())) {
			if (propFeePollfeeImportLog.getLink().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"错误报告日志链接长度超出");
			}
		}

		propFeePollfeeImportLog.setImportLogID(IdUtil.getuuId());
		propFeePollfeeImportLog.setItemStat(TypeConst.FLAG_ON);
		propFeePollfeeImportLog.setTscreate(new Date());
		propFeePollfeeImportLog.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeePollfeeImportLogMapper.insertSelective",
				propFeePollfeeImportLog);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeePollfeeImportLog(
			PropFeePollfeeImportLogDto propFeePollfeeImportLog)
			throws AppBizException {

		Assert.notNull(propFeePollfeeImportLog);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeePollfeeImportLog.getImportLogID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeePollfeeImportLogDto oldPropFeePollfeeImportLog = this
				.findPropFeePollfeeImportLogByID(propFeePollfeeImportLog
						.getImportLogID());

		if (!StringUtils.isBlank(propFeePollfeeImportLog.getFileName())) {
			if (propFeePollfeeImportLog.getFileName().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "文件名称长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeePollfeeImportLog.getStatus())) {
			if (propFeePollfeeImportLog.getStatus().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"1导入中,2上传成功，3上传失败长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeePollfeeImportLog.getImportInfo())) {
			if (propFeePollfeeImportLog.getImportInfo().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "上传信息长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeePollfeeImportLog.getLink())) {
			if (propFeePollfeeImportLog.getLink().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"错误报告日志链接长度超出");
			}
		}

		if (oldPropFeePollfeeImportLog == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeePollfeeImportLog.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeePollfeeImportLogMapper.updateByPrimaryKeySelective",
				propFeePollfeeImportLog);
	}

	@Override
	public PropFeePollfeeImportLogDto findPropFeePollfeeImportLogByID(
			String propFeePollfeeImportLogID) throws AppBizException {
		if (StringUtils.isBlank(propFeePollfeeImportLogID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeePollfeeImportLogDto oldPropFeePollfeeImportLog = this
				.getSqlSession()
				.selectOne(
						"PropFeePollfeeImportLogSearchMapper.selectByPrimaryKey",
						propFeePollfeeImportLogID);
		if (oldPropFeePollfeeImportLog == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeePollfeeImportLog.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeePollfeeImportLog;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeePollfeeImportLogByLogic(
			String propFeePollfeeImportLogID) throws AppBizException {

		if (StringUtils.isBlank(propFeePollfeeImportLogID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeePollfeeImportLogDto oldPropFeePollfeeImportLog = this
				.findPropFeePollfeeImportLogByID(propFeePollfeeImportLogID);

		oldPropFeePollfeeImportLog.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeePollfeeImportLog.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeePollfeeImportLogMapper.updateByPrimaryKeySelective",
				oldPropFeePollfeeImportLog);
	}

	@Override
	public List<Map<String, Object>> findPropFeePollfeeImportLogByPage(
			String commID, String tsCreate, String search, int pageNo,
			int pageSize) throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("commID", commID);
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeePollfeeImportLogSearchMapper.findAllPropFeePollfeeImportLog",
						map);
		return list;
	}

	@Override
	public List<PropFeePollfeeImportLogDto> getPropFeePollfeeImportLogByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeePollfeeImportLogDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeePollfeeImportLogSearchMapper.getAllPropFeePollfeeImportLogDto",
						map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeePollfeeImportLogByRelevantID(
			String importLogID, String status, String commID)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("importLogID", importLogID);
		map.put("status", status);
		map.put("commID", commID);

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeePollfeeImportLogSearchMapper.selectPropFeePollfeeImportLogDtoyRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeePollfeeImportLogDto> findPropFeePollfeeImportLogByRelevantID(
			String importLogID, String status, String commID)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("importLogID", importLogID);
		map.put("status", status);
		map.put("commID", commID);

		List<PropFeePollfeeImportLogDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeePollfeeImportLogSearchMapper.findPropFeePollfeeImportLogDtoByRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeePollfeeImportLogDto> bauthfindPropFeePollfeeImportLog(
			List<String> propFeePollfeeImportLogIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeePollfeeImportLogDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeePollfeeImportLogSearchMapper.batchFindPropFeePollfeeImportLog",
						propFeePollfeeImportLogIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeePollfeeImportLogByMap(
			Map<String, Object> propFeePollfeeImportLog) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeePollfeeImportLogSearchMapper.batchFindPropFeePollfeeImportLogByMap",
						propFeePollfeeImportLog);
		return list;
	}

	@Override
	public void clearCache() throws AppBizException {
		System.out.println("清除缓存成功");
	}

}